package com.seafwg.eduservice.controller;


import com.seafwg.commonutils.R;
import com.seafwg.eduservice.entity.EduChapter;
import com.seafwg.eduservice.entity.chapter.ChapterVo;
import com.seafwg.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author seafwg
 * @since 2021-01-19
 */
@Api(description = "章节管理模块")
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    /**
     * 课程大纲列表,根据课程id进行查询
     * @param courseId
     * @return
     */
    @GetMapping("/getChapterVideo/{courseId}")
    @ApiOperation(value = "课程大纲列表,根据课程id进行查询")
    public R getChapterVideo(@PathVariable String courseId) {
        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("chapterVideo", list);
    }

    /**
     * 添加课程大纲：
     * @param eduChapter
     * @return
     */
    @PostMapping("/addChapter")
    @ApiOperation(value = "添加课程大纲")
    public R addChapter(@RequestBody EduChapter eduChapter) {
        // mybatisPlus 提供的save方法
        chapterService.save(eduChapter);
        return R.ok();
    }

    /**
     * 修改章节：
     * @param eduChapter
     * @return
     */
    @PostMapping("/updateChapter")
    @ApiOperation(value = "修改章节")
    public R updateChapter(@RequestBody EduChapter eduChapter) {
        // mybatisPlus 提供的save方法
        chapterService.updateById(eduChapter);
        return R.ok();
    }

    /**
     * 根据章节id查询章节信息
     * @param chapterId
     * @return
     */
    @GetMapping("/getChapterInfo/{chapterId}")
    @ApiOperation(value = "根据id查询章节信息")
    public R getChapterInfoById(@PathVariable String chapterId) {
        EduChapter eduChapter = chapterService.getById(chapterId);
        return R.ok().data("chapter", eduChapter);
    }

    /**
     * 根据章节id删除章节信息
     * @param chapterId
     * @return
     */
    @DeleteMapping("/{chapterId}")
    @ApiOperation(value = "根据章节id删除章节信息")
    public R deleteChapter(@PathVariable String chapterId) {
        boolean flag = chapterService.deleteChapter(chapterId);
        if(flag) {
            return R.ok();
        }else{
            return R.ok();
        }
    }
}

